#include "./view.h"

/*********SGUI 进度条模板*******
占用资源：
obj->data[4] 回调函数
obj->data[5] 控制Obj
obj->data[6] 控制大小
obj->data[7] 进度
obj->data[8] 进度条高度
obj->data[9] 进度最大值
******************************/

//绘制拖拽盘
void sGuiDrawDragPad(void*ob)
{
 sObj*obj = ob;
 sCam*cam = &(_room->cam);
 int x = obj->x-cam->x;
 int y = obj->y-cam->y;
 int r = obj->sprw/2;
 if(obj->data[6])
 {
  drawCir(x+r,y+r,r,
   (0xff60aef0&0xffffff)|0x40000000);
  drawCir(x+r,y+r,
   r*7/12,0xff60aef0);
 }
 else
 	drawCir(x+r,y+r,
  r*13/24,0xff60aef0);
}

//监听拖动按钮
void sGuiSeekClick(sObj*obj,int act)
{
 sObj*obj2;
 obj2 = (sObj*)obj->data[5];
 if(act)
 {
  obj->x = maxInt(obj2->x-
   obj->sprw/2,obj->x);
  obj->x = minInt(obj2->x+
   obj2->sprw-obj->sprw/2,obj->x);
  obj->y = obj2->y+
  obj2->sprh/2-obj->sprw/2;
  sGuiSetSeekBar(obj2,
   (obj->x+obj->sprw/2-obj2->x)*
   obj2->data[9]/obj2->sprw);
 }
 //调用回调函数
 if(obj2->data[4]!=-1)
 {
  sDragClick click = (sDragClick)
  obj2->data[4];
  (*click)(obj,act);
 }
}

//创建拖拽条
sObj*sGuiNewSeekBar(int x,int y,int w,int h,int max,int dw,sDragClick click)
{
 sObj*obj = sNewObj(x,y,NULL);
 obj->id = 1;
 obj->sprw = w;
 obj->sprh = h;
 obj->draw = sGuiDrawSeekBar;
 obj->data[4] = (int)click;
 obj->data[6] = dw*2;
 obj->data[7] = 0;
 obj->data[8] = h;
 obj->data[9] = max;
 
 y = obj->y+obj->sprh/2-dw;
 sObj*obj2 = 
 sGuiNewDragView(x-dw,y,
  NULL,sGuiSeekClick,TRUE);
 obj2->id = 2;
 obj2->sprw = dw*2;
 obj2->sprh = dw*2;
 obj2->draw = sGuiDrawDragPad;
 obj2->data[5] = (int)obj;
 
 obj->data[5] = (int)obj2;
 return obj;
}

//获取控制钮
sObj*sGuiGetSeekDrag(sObj*obj)
{
 return(sObj*)obj->data[5];
}

//创建进度条
sObj*sGuiNewSeekView(int x,int y,int w,int h,int max)
{
 sObj*obj = sNewObj(x,y,NULL);
 obj->id = 1;
 obj->sprw = w;
 obj->sprh = h;
 obj->draw = sGuiDrawSeekBar;
 obj->data[7] = 0;
 obj->data[8] = h;
 obj->data[9] = max;
 return obj;
}

//设置进度
void sGuiSetSeekBar(sObj*obj,int load)
{
 int max = obj->data[9];
 load = maxInt(0,load);
 load = minInt(max,load);
 obj->data[7] = load;
}

//获取进度
int sGuiGetSeekBar(sObj*obj)
{
 int load = obj->data[7];
 int max = obj->data[9];
 load = maxInt(0,load);
 load = minInt(max,load);
 return load;
}

//绘制进度
void sGuiDrawSeekBar(void*ob)
{
 sObj*obj = ob;
 sCam*cam = &(_room->cam);
 sDrawObjCam(obj,cam);
 int x = obj->x;
 int w = obj->sprw;
 int h = obj->data[8];
 int y = obj->y+(obj->sprh-h)/2;
 int max = obj->data[9];
 int load = obj->data[7];
 load = maxInt(0,load);
 load = minInt(max,load);
 int dw = w*load/max;
 drawRect(x-cam->x,y-cam->y,
  w,h,0xb0808080);
 drawRect(x-cam->x,y-cam->y,
  dw,h,0xff7ec0f0);
}


